package ctf.evaluation.SimulatorTests;

import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;

import model.GameConstants;

import ctf.evaluation.Point;
import ctf.evaluation.TestCase;
import ctf.evaluation.simulator.ConnectionManager;
import ctf.evaluation.simulator.ConnectionManager.STEALState;
import ctf.evaluation.simulator.data.Player;
import ctf.evaluation.simulator.data.State;
import ctf.evaluation.simulator.responses.CommandResponse;
import ctf.evaluation.simulator.responses.GameOverException;
import ctf.evaluation.simulator.responses.RoundOverException;
import ctf.evaluation.simulator.responses.StateResponse;
import ctf.model.Side;
import ctf.network.NetworkException;
import ctf.network.ProtocolError;
import ctf.network.SocketClosedException;
import ctf.network.UninitializedSocketException;
import ctf.network.tcp.Connection;

public class AttackingStateTests extends TestCase {
	private ConnectionManager connections = ConnectionManager.instance();
	private Connection red, blue;
	private Rectangle leftJail, rightJail;
	@Override
	public void setUp() throws Exception {
		connections.enterState(STEALState.PLAY);
		red = connections.red();
		blue = connections.blue();
		leftJail = new Rectangle(0, 0, 25, 25);
		rightJail = new Rectangle(475, 0, 25, 25);
		
	}
	
	@Override public void tearDown() {};
	
	/**
	 * If an attacking player touches a defending player, he becomes  
	 * @throws IOException
	 * @throws NetworkException
	 * @throws ProtocolError
	 * @throws RoundOverException
	 * @throws GameOverException
	 */
	public void testAttackingPlayerDefendingPlayer() throws IOException, NetworkException, ProtocolError, RoundOverException, GameOverException{
		red.sendLine("accelerate 0 0 2.0");
		CommandResponse response = new CommandResponse(red);
		sleep(22000);
		red.sendLine("getState");
		State state = (new StateResponse(red)).state();
		Player player = state.team(Side.LEFT).player(0);
		assertTrue(playerInJail(player));
		assertEquals(Player.State.PRISONER, player.state());
	}
	
	/**
	 * 
	 * @throws IOException
	 * @throws NetworkException
	 * @throws ProtocolError
	 * @throws GameOverException
	 * @throws RoundOverException
	 */
//	public void testAttackingPlayerJailTouch() throws IOException, NetworkException, ProtocolError,  GameOverException, RoundOverException{
//		Point playerPosition = new Point(472.5, 12.5);
//		red.sendLine("placePlayer 0 " + playerPosition .toSTEAL());
//		CommandResponse response = new CommandResponse(red);
//
//		red.sendLine("accelerate 0 0 2.0");
//		response = new CommandResponse(red);
//		sleep(30000);
//		
//		red.sendLine("getState");
//		State state = (new StateResponse(red)).state();
//		Player player = state.team(Side.LEFT).player(0);
//		assertEquals(Player.State.ESCAPEE, player.state());
//		sleep(32000);
//		
//		red.sendLine("getState");
//		State redState = (new StateResponse(red)).state();		
//		player = redState.team(Side.LEFT).player(0);
//		
//		assertEquals(Player.State.PRISONER, player.state());
//		assertTrue(playerInJail(player));
//
//	}

	private boolean playerInJail(Player player) {
		return leftJail.contains(player.location().x, player.location().y) 
				|| (rightJail.contains(player.location().x, player.location().y));
	}
}
